from django.db import models
from apps.article.models import Article
from apps.system.models import Upload

# Create your models here.
from utils.exceptions import CustomError


class AdornProcess(models.Model):
    title = models.CharField(verbose_name='标题', max_length=10)
    notes = models.CharField(verbose_name='备注', max_length=100)
    bg_color = models.CharField(verbose_name='颜色', max_length=50)
    icon = models.CharField(verbose_name='图标', max_length=50)
    thumbnail = models.ForeignKey(Upload, verbose_name='缩略图', on_delete=models.PROTECT,
                                  related_name='adorn_process_thumbnail')
    describe = models.TextField(verbose_name='内容')
    related_articles = models.ManyToManyField(Article, verbose_name='关联文章', blank=True)

    class Meta:
        db_table = 'adorn_process'
        verbose_name = '装修流程'

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的装修流程id')

        instance = AdornProcess.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的装修流程实例')
        return instance
